import sys, urllib2, time, os , Queue, threading,re
 
def cslogo():
    print '''
  ___ ___ ____ ____ ____ __ __ _ _
 / __)/ _ \( _ \( ___)( _ \( ) /__\ ( \/ )
( (__( (_) ))(_) ))__) )___/ )(__ /(__)\ \ /
 \___)\___/(____/(____)(__) (____)(__)(__)(__)
 Name:get title exploit
 
'''
 
# show message
def msg(text, type=0):
    if type == 0:
       str_def = "[*]"
    elif type == 1:
       str_def = "[+]"
    else:
       str_def = "[-]";
    print str_def + text;
 
def find_text(text, start, end):
    regex = '%s(.*?)%s' % (start, end)
    text_re = re.search(regex, text)
    if text_re is None :
        return "none"
    return text_re.group(1)
 
def gbk2utf8(text):
    data=unicode(text,"gb2312")
    return data.encode("utf-8")
 
# get url data
def get(url):
    try:
      r = urllib2.urlopen(url, timeout=20)
      return r.read()
    except :
      return "none"
 
def post(url,data):
 try:
    r = urllib2.urlopen(url,data,timeout=30)
    return r.read();
 except urllib2.HTTPError, error:
    return error.read()
def file_to_arr(file):
    arr=[]
    f = open(file)
    for line in f:
      arr.append(line.strip())
    return arr
 
 
def exploit(i):
    html=get("http://"+i).lower()
    if html != "none" :
     print i+" - "+find_text(html,"<title>","</title>")+"\r";
   
    return
class Worker(threading.Thread):
    def __init__(self):
      threading.Thread.__init__(self)
    def run(self):
        while 1:
         if data_queue.empty() == True:
             break
         exploit(data_queue.get());
 
 
if __name__ == '__main__':
 
   cslogo()
   msg("Get Title exploit")
 
   file=sys.argv[1]
   arr=file_to_arr(file)
   thread=int(sys.argv[2])
   thread= thread if thread< len(arr) else len(arr)
   msg("file:%s iplist:%d thread:%s" % (file,len(arr),thread))
   global data_queue
   data_queue = Queue.Queue()
   msg("generate ip queue ...")
   for i in arr :
      data_queue.put(i)
   for t in range(thread):
       Worker().start()
   sys.exit(0) 
